<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;

/**
 * 首页
 */
class StudyController extends HomebaseController {

    protected $shop_model;
    protected $shop_size;
    protected $study_model;
    protected $slide_model;

    function _initialize() {
        parent::_initialize();
        $this->shop_model = D("Common/Shop");
        $this->shop_size = D("Common/ShopSize");
        $this->study_model = D("Common/Study");
        $this->slide_model = D("Common/Slide");
    }

	public function index() {
        $slides = $this->slide_model->where(array('slide_status'=>1,'slide_pic'=>array('neq','')))->field('slide_pic')->select();
        $studys = $this->study_model->where(array('study_status'=>1,'study_pic'=>array('neq','')))->field('study_name,study_pic')->select();
        $this->assign('slides',$slides);
        $this->assign('studys',$studys);
        $this->display(':index');
    }

    //订购验证
    public function check(){
        $this->display(':check');
    }

    public function post(){
        $study_code = I('study_code');
        $study_id = $this->study_model->where(array('study_code'=>$study_code))->getField('study_id');
        if(!$study_id){
            echo '-1';
        }else{
            cookie('study_id',$study_id,array('expire'=>1800));
            echo $study_id;
        }
    }

    //订购页
    public function study(){
        $study_id = I('study_id');
        $shop_sex = I('shop_sex');
        $study_name = $this->study_model->where(array('study_id'=>$study_id))->getField('study_name');
        $slides = $this->slide_model->where(array('slide_status'=>1))->field('slide_pic')->select();
        $shops = $this->shop_model->where(array('study_id'=>$study_id,'shop_sex'=>$shop_sex))->select();
        foreach ($shops as $k =>$v){
            $shops[$k]['study_name'] = $study_name;
            $shops[$k]['shop_size'] = json_decode($v['shop_size'],true);
            $shops[$k]['smeta'] = json_decode($v['smeta'],true);
        }
        $this->assign('slides',$slides);
        $this->assign('shops',$shops);
        $this->display(':study');
    }

    //校服订购提交
    public function shoppost(){
        $study_id = I('study_id');
        $endtime = $this->study_model->where(array('study_id'=>$study_id))->getField('end_time');
        $c = (strtotime($endtime)) - time();
        if(cookie('study_id') == null){
            $data['status'] = 3;
            $data['msg'] = '请重新选择学校';
        }elseif($c <= 0){
            $data['status'] = 2;
            $data['msg'] = '购买时间已过';
        }
        echo json_encode($data);
    }
}